//
//  BRPayDetailPopView.m
//  Kexu
//
//  Created by IOS on 2023/7/19.
//

#import "BRPayDetailPopView.h"

@implementation BRPayDetailPopView

- (instancetype)initWithType:(BRPAYMENT_TYPE)type isLost:(BOOL)isLost
{
    self = [super init];
    if (self) {
        _isLost = isLost;
        [self setUI:type];
    }
    return self;
}

- (void)hidden {
    
    [self hidden:0.25];
   
}

- (void)hidden:(CGFloat)time {
    
    [UIView animateWithDuration:time animations:^{
      
        self.shadowView.alpha = 0;
        self.contView.transform = CGAffineTransformMakeRotation(0);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.hidden = YES;
    });
    if (self.hiddenBlock) self.hiddenBlock();
}

- (void)show {
    

    CGFloat h = self.contView.mj_h - 40;
    self.hidden = NO;
    [UIView animateWithDuration:0.25 animations:^{
        
        self.shadowView.alpha = 1;
        self.contView.transform = CGAffineTransformMakeTranslation(0, h);
    }];
}

- (void)br_topStackText:(NSString *)text by:(NSInteger)index {
    BRImgLabelView *label = [self.topStack1 viewWithTag:300 + index];
    label.titleL.text = text;
}

- (void)br_topStackTextAtt:(NSAttributedString *)attText by:(NSInteger)index {
    BRImgLabelView *label = [self.topStack1 viewWithTag:300 + index];
    label.titleL.attributedText = attText;
}

- (void)br_topStackTextHidden:(BOOL)hidden by:(NSInteger)index {
    BRImgLabelView *label = [self.topStack1 viewWithTag:300 + index];
    label.hidden = hidden;
}


- (void)setHiddenALL:(BOOL)hiddenALL {
    _hiddenALL = hiddenALL;
    
    self.line.hidden = hiddenALL;
    self.topStack1.hidden = hiddenALL;
    CGFloat marginX = 20;
    if (hiddenALL) {
        [self.centStack mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(marginX);
            make.right.offset(-marginX);
            make.top.offset(48);
        }];
    }else {
        [self.centStack mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(marginX);
            make.right.offset(-marginX);
            make.top.equalTo(self.topStack1.mas_bottom).offset(40);
        }];
    }
}

#pragma mark - UI --
- (void)setUI:(BRPAYMENT_TYPE)type {
    
    self.backgroundColor = UIColor.clearColor;
    
    [self addSubview:self.shadowView];
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    CGFloat content_y = 134 + kSafeAreaTopHeight + 8;
    // 有优惠券
    if (type == BRPAY_Rent && !self.isLost) {
        content_y += 44;
    }
    [self addSubview:self.contView];
    [self.contView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.right.offset(-14);
        make.bottom.equalTo(self.mas_top).offset(content_y);
    }];
    
    CGFloat marginX = 20;
    [self.contView addSubview:self.topStack1];
    [self.topStack1 br_addSubLabel:@[@"--",@"--",@"--",@"--"] font:[UIFont fontM:14] textColor:kColorTitle66B];
    [self.topStack1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(marginX);
        make.right.offset(-marginX);
        make.top.offset(48);
    }];
    
    [self.topStack1 br_img:@"charge_type" by:0];
//    [self.contView addSubview:self.topStack2];
//    [self.topStack2 br_addSubLabel:@[@"--",@"--"] font:[UIFont fontM:14] textColor:kColorTitle66B];
//    [self.topStack2 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(marginX);
//        make.right.offset(-marginX);
//        make.top.equalTo(self.topStack1.mas_bottom).offset(10);
//    }];
    
    [self.contView addSubview:self.centStack];
    [self.centStack br_addSubLabel:@[@"--",@"--",@"--",@"--",@"--",@"--",@"--"] font:[UIFont fontM:14] textColor:kColorTitle66B];
    [self.centStack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(marginX);
        make.right.offset(-marginX);
        make.top.equalTo(self.topStack1.mas_bottom).offset(40);
    }];
    
    QMUILabel *titleL = [BRToolsView br_label:Localized(@"home_billingRules") textColor:kColorTitle font:[UIFont fontS:15]];
   
    self.textPlan = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontR:13]];
    self.textPlan.numberOfLines = 0;
    
    [self.contView addSubview:titleL];
    
    [self.contView addSubview:self.textPlan];
    
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(marginX);
        make.right.offset(-marginX);
        make.top.equalTo(self.centStack.mas_bottom).offset(40);
    }];
    [self.textPlan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(marginX);
        make.right.offset(-marginX);
        make.top.equalTo(titleL.mas_bottom).offset(4);
        make.bottom.offset(-20);
    }];
    
    self.line = [BRToolsView getLineView:UIColorMakeWithHex(@"#D2D8E2")];
    [self.contView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(self.topStack1.mas_bottom).offset(20);
        make.height.mas_equalTo(0.5);
    }];
    {
        UIView *line = [BRToolsView getLineView:UIColorMakeWithHex(@"#D2D8E2")];
        [self.contView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            make.top.equalTo(self.centStack.mas_bottom).offset(20);
            make.height.mas_equalTo(0.5);
        }];
    }
}

- (UIVisualEffectView *)shadowView {
    
    if (_shadowView == nil) {
        
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        
        _shadowView = [[UIVisualEffectView alloc] initWithEffect:effect];
        _shadowView.alpha = 0;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidden)];
        [_shadowView addGestureRecognizer:tap];
    }
    
    return _shadowView;
}

- (UIView *)contView {
    
    if (_contView == nil) {
        _contView = [[UIView alloc] init];
        _contView.backgroundColor = kColorBGEE;
        _contView.layer.cornerRadius = 26;
        _contView.layer.masksToBounds = YES;
    }
    return _contView;
}

- (BRStackImgLabelView *)topStack1 {
    
    if (_topStack1 == nil) {
        
        _topStack1 = [[BRStackImgLabelView alloc] init];
        
    }
    return _topStack1;
}

- (BRStackLabelView *)topStack2 {
    if (_topStack2 == nil) {
        _topStack2 = [[BRStackLabelView alloc] init];
    }
    return _topStack2;
}
- (BRStackLabelView *)centStack {
    if (_centStack == nil) {
        _centStack = [[BRStackLabelView alloc] init];
    }
    return _centStack;
}


@end
